﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Ignitron.EEngine.Lighting;
using SharpDX;
using Ignitron.EEngine.Adapters.DX.Helper;

namespace Ignitron.EEngine.Adapters.DX.Graphics
{
    /// <summary>
    /// Lighting Manager class
    /// </summary>
    internal class LightingManager
    {
        /// <summary>
        /// The list of lights
        /// </summary>
        private const int mMaxLights = 8;
        private int mLightCount;
        private ELight[] mLights = new ELight[mMaxLights];

        private Vector4[] mLightPositions = new Vector4[mMaxLights];

        /// <summary>
        /// The global ambient light
        /// </summary>
        private Vector3 mGlobalAmbientLight;

        private EMaterial mMaterial;

        /// <summary>
        /// Constructor
        /// </summary>
        public LightingManager()
        {
            LightingEnabled = false;
        }

        public bool LightingEnabled
        { get; set; }

        /// <summary>
        /// Returns the maximum number of lights
        /// </summary>
        public int MaxLights
        {
            get { return mMaxLights; }
        }

        /// <summary>
        /// Returns the real count of lights
        /// </summary>
        public int LightCount
        {
            get { return mLightCount; }
        }

        /// <summary>
        /// Creates a light
        /// </summary>
        public ELight CreateLight()
        {
            if (mLightCount >= mMaxLights)
                throw new ApplicationException("Light count overflow.");

            ELight light = new ELight(mLightCount);
            mLights[mLightCount++] = light;
            return light;
        }

        /// <summary>
        /// Saves the light's world position
        /// </summary>
        /// <param name="light"></param>
        public void RenderLight(ELight light, Matrix worldMatrix)
        {
            mLightPositions[light.LightIndex] = Vector4.Transform(light.Position.ToDX(), worldMatrix);
        }

        /// <summary>
        /// Sets the current rendering material
        /// </summary>
        public void SetMaterial(EMaterial material)
        {
            mMaterial = material;
        }

        /// <summary>
        /// Returns the material
        /// </summary>
        public EMaterial GetMaterial()
        {
            return mMaterial;
        }

        /// <summary>
        /// Returns the light's position
        /// </summary>
        public Vector4 GetLightPosition(int index)
        {
            return mLightPositions[index];
        }

        /// <summary>
        /// Returns the light at specified index
        /// </summary>
        public ELight GetLight(int index)
        {
            return mLights[index];
        }

        /// <summary>
        /// Sets the global ambient
        /// </summary>
        public void SetGlobalAmbientLight(Vector3 ambient)
        {
            mGlobalAmbientLight = ambient;
        }

        /// <summary>
        /// Gets the global ambient light
        /// </summary>
        /// <returns></returns>
        public Vector3 GetGlobalAmbientLight()
        {
            return mGlobalAmbientLight;
        }

    }
}
